package huixue.cn.com.rxapp.widget.kline;

import android.content.Context;
import android.graphics.Canvas;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import huixue.cn.com.rxapp.widget.kline.ChartConstant;

import huixue.cn.com.library.util.LogUtils;

/**
 * Created by Administrator on 2018/8/14.
 */

public abstract class ChartView extends View{
    /**
     * 分时线或K线类型，取值见ChartConstant.TYPE_FENSHI
     */
    protected int lineType = ChartConstant.TYPE_FENSHI;
    /**
     * 指标的类型
     */
    protected int indexType = ChartConstant.INDEX_VOL;
    //分时图,K线高度
    protected float mainH;
    //指标高度
    protected float indexH;

    //指标左上y坐标
    protected float indexStartY;
    //时间左上Y坐标
    protected float timeStartY;

    //总宽
    protected float mWidth;
    //总高
    protected float mHeight;

    public ChartView(Context context) {
        super(context);
        //关闭硬件加速，不然虚线显示为实线了
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    public ChartView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        //关闭硬件加速，不然虚线显示为实线了
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        initWidthAndHeight();
        //2，画网格
        drawGrid(canvas);
        //3，画线（分时线的价格线、均价线或K线的均线）
        drawLines(canvas);
        //4，如果是K线另外画烛形图
        drawCandles(canvas);

        //5，写上XY轴的文字（写早了会被覆盖）
        drawText(canvas);
        //6，画需要显示的指标
        switch (indexType) {
            case ChartConstant.INDEX_VOL:
                drawVOL(canvas);
                break;
            case ChartConstant.INDEX_ZJ:
                drawZJ(canvas);
                break;
            case ChartConstant.INDEX_MACD:
                drawMACD(canvas);
                break;
            case ChartConstant.INDEX_KDJ:
                drawKDJ(canvas);
                break;
        }
    }

    private void initWidthAndHeight() {
        mHeight = getMeasuredHeight();
        mWidth = getMeasuredWidth();
        mainH = mHeight * ChartConstant.MAIN_SCALE;
        indexH = mHeight * ChartConstant.INDEX_SCALE;
        indexStartY = mHeight - indexH;
        timeStartY = indexStartY - mHeight * ChartConstant.TIME_SCALE;
        init();

        LogUtils.e("mHeight=="+mHeight);
        LogUtils.e("mWidth=="+mWidth);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);




    }

    /**
     * 画网格包含主网格和指标网格
     * @param canvas
     */
    protected abstract void drawGrid(Canvas canvas);

    /**
     * 写X，Y轴文字
     * @param canvas
     */
    protected abstract void drawText(Canvas canvas);

    /**
     * 画量
     * @param canvas
     */
    protected abstract void drawVOL(Canvas canvas);

    /**
     * 话资金
     * @param canvas
     */
    protected abstract void drawZJ(Canvas canvas);
    /**
     *在开始画图前，初始化数据
     */
    protected abstract void init();

    protected void drawMACD(Canvas canvas) {}
    protected void drawKDJ(Canvas canvas) {}

    /**
     * 分时价格线均线、K线的均线
     * @param canvas
     */
    protected void drawLines(Canvas canvas) {}
    protected void drawCandles(Canvas canvas) {}


}
